package com.sakura.mall.product.attr.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.product.attr.domain.entity.AttrEntity;
import com.sakura.mall.product.attr.domain.request.AddAttrRequest;
import com.sakura.mall.product.attr.domain.request.UpdateAttrRequest;
import com.sakura.mall.product.attr.domain.request.AttrPageRequest;
import com.sakura.mall.product.attr.domain.vo.AttrVo;
import com.sakura.mall.product.attr.mapper.AttrMapper;
import com.sakura.mall.product.attr.service.AttrService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品属性 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class AttrServiceImpl extends ServiceImpl<AttrMapper, AttrEntity> implements AttrService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddAttrRequest request) {
        // todo 其他校验逻辑
        AttrEntity attr = request.convert(AttrEntity.class);
        this.save(attr);
    }

    @Override
    public Page<AttrVo> queryPage(AttrPageRequest request) {
        IPage<AttrEntity> qp = request.toPage();
        LambdaQueryWrapper<AttrEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<AttrVo> ret = qp.convert(entity -> entity.convert(AttrVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateAttrRequest request) {
        AttrEntity entity = request.convert(AttrEntity.class);
        entity.setAttrId(id);
        this.updateById(entity);
    }

}

